#include "BattleManager.h"
#include "MapManager.h"
#include "CreatureManager.h"
#include "msgDefine.h"

BattleManager* gBattleManager = F_NULL;
RECV_MSG_BATTLE gRecvMsgBattle;

BattleManager::BattleManager()
{
	gBattleManager = this;
}


BattleManager::~BattleManager()
{

}


fvoid				BattleManager::Init()
{
	for ( fint32 i = 0 ; i < SESSION_CACHE_COUNT ; i++ )
	{
		mBattleSession[i] = new BattleSession();
		mFreeSession.push_back( mBattleSession[i] );
	}
}


fvoid				BattleManager::Release()
{
	for ( fint32 i = 0 ; i < SESSION_CACHE_COUNT ; i++ )
	{
		delete mBattleSession[i];
	}

	delete mThis;
	mThis = F_NULL;
}

fbool				BattleManager::CreatBattle( GamePlayer* player1 , GamePlayer* player2 )
{
    // 这里要改，，
    
	BattleSession* session = CreatSession();

	if ( !session )
	{
		return F_FALSE;
	}

	PlayerSessionMapIter iter = mPlayerMap.find( player1 );
	if ( iter != mPlayerMap.end() )
	{
		assert( 0 );
		return F_FALSE;
	}

	mPlayerMap[ player1 ] = session;

	iter = mPlayerMap.find( player2 );
	if ( iter != mPlayerMap.end() )
	{
		assert( 0 );
		return F_FALSE;
	}

	mPlayerMap[ player2 ] = session;

	session->InitPVP( player1 , player2 );

	gRecvMsgBattle.PVP = 1;
	gRecvMsgBattle.Side = (fbyte)SABSS_SELF;
	gRecvMsgBattle.PetNum = 0;
	GameTeam* team = player1->GetTeam();

	if ( team )
	{
		if ( !team->IsLeader( player1 ) )
		{
			assert( 0 );
			return F_FALSE;
		}

		fint32 count = team->GetMemberCount();
		for ( fint32 i = 0 ; i < count ; i++ )
		{
			GamePet* pet = ((GamePlayer*)team->mMembers[i])->GetAttackPet();

			if ( pet )
			{
				CreatureData& data = pet->GetData();
				CreatureFightData& fightData = pet->GetFightData();

				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].ActionID = data.ActionID;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Role = data.Role;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Color = data.Color;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Level = data.Level;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Index = i;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].HP = fightData.Base.HP;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].MAXHP = fightData.Base.MAXHP;

				gRecvMsgBattle.PetNum++;
			}

		}
	}
	else
	{
		GamePet* pet = player1->GetAttackPet();

		if ( pet )
		{
			CreatureData& data = pet->GetData();
			CreatureFightData& fightData = pet->GetFightData();

			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].ActionID = data.ActionID;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Role = data.Role;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Color = data.Color;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Level = data.Level;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Index = 0;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].HP = fightData.Base.HP;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].MAXHP = fightData.Base.MAXHP;

			gRecvMsgBattle.PetNum++;
		}
	}


	team = player2->GetTeam();

	if ( team )
	{
		if ( !team->IsLeader( player2 ) )
		{
			assert( 0 );
			return F_FALSE;
		}

		fint32 count = team->GetMemberCount();
		gRecvMsgBattle.EnemyNum = count;
		gRecvMsgBattle.EnemyPetNum = 0;

		for ( fint32 i = 0 ; i < count ; i++ )
		{
			CreatureData& data = team->GetMember( i )->GetData();
			CreatureFightData& fightData = team->GetMember( i )->GetFightData();

			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].ActionID = data.ActionID;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].Role = data.Role;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].Color = data.Color;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].Level = data.Level;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].Index = i;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].HP = fightData.Base.HP;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].MAXHP = fightData.Base.MAXHP;
		}

		for ( fint32 i = 0 ; i < count ; i++ )
		{
			GamePet* pet = ((GamePlayer*)team->mMembers[i])->GetAttackPet();

			if ( pet )
			{
				CreatureData& data = pet->GetData();
				CreatureFightData& fightData = pet->GetFightData();
				
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].ActionID = data.ActionID;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].Role = data.Role;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].Color = data.Color;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].Level = data.Level;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].Index = i;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].HP = fightData.Base.HP;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].MAXHP = fightData.Base.MAXHP;

				gRecvMsgBattle.EnemyPetNum++;
			}
		}
	}
	else
	{
		CreatureData& data = player2->GetData();
		CreatureFightData& fightData = player2->GetFightData();

		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].ActionID = data.ActionID;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Role = data.Role;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Color = data.Color;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Level = data.Level;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Index = 0;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].HP = fightData.Base.HP;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].MAXHP = fightData.Base.MAXHP;


		GamePet* pet = player2->GetAttackPet();

		if ( pet )
		{
			CreatureData& data = pet->GetData();
			CreatureFightData& fightData = pet->GetFightData();

			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].ActionID = data.ActionID;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].Role = data.Role;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].Color = data.Color;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].Level = data.Level;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].Index = 0;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].HP = fightData.Base.HP;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].MAXHP = fightData.Base.MAXHP;

			gRecvMsgBattle.EnemyPetNum = 1;
		}

		gRecvMsgBattle.EnemyNum = 1;
	}

	gRecvMsgBattle.Pos = player1->mMoveData.Pos;
	gRecvMsgBattle.size = 17 + ( gRecvMsgBattle.PetNum + gRecvMsgBattle.EnemyNum + gRecvMsgBattle.EnemyPetNum )* sizeof( MsgCreature );

	player1->PackMsg( &gRecvMsgBattle );
	player1->SendMsg();
	player1->StartFight();
	team = player1->GetTeam();

	if ( team )
	{
		fint32 count = team->GetMemberCount();

		for ( fint32 i = 1 ; i < count ; i++ )
		{
			team->mMembers[ i ]->PackMsg( &gRecvMsgBattle );
			team->mMembers[ i ]->SendMsg();
			team->mMembers[ i ]->StartFight();
		}
	}


    // player2...

	gRecvMsgBattle.PetNum = 0;
	gRecvMsgBattle.Side = (fbyte)SABSS_SELF;
	team = player2->GetTeam();

	if ( team )
	{
		if ( !team->IsLeader( player2 ) )
		{
			assert( 0 );
			return F_FALSE;
		}

		fint32 count = team->GetMemberCount();
		for ( fint32 i = 0 ; i < count ; i++ )
		{
			GamePet* pet = ((GamePlayer*)team->mMembers[i])->GetAttackPet();

			if ( pet )
			{
				CreatureData& data = pet->GetData();
				CreatureFightData& fightData = pet->GetFightData();

				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].ActionID = data.ActionID;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Role = data.Role;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Color = data.Color;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Level = data.Level;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Index = i;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].HP = fightData.Base.HP;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].MAXHP = fightData.Base.MAXHP;

				gRecvMsgBattle.PetNum++;
			}

		}
	}
	else
	{
		GamePet* pet = player2->GetAttackPet();

		if ( pet )
		{
			CreatureData& data = pet->GetData();
			CreatureFightData& fightData = pet->GetFightData();

			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].ActionID = data.ActionID;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Role = data.Role;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Color = data.Color;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Level = data.Level;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Index = 0;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].HP = fightData.Base.HP;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].MAXHP = fightData.Base.MAXHP;

			gRecvMsgBattle.PetNum++;
		}
	}


	team = player1->GetTeam();

	gRecvMsgBattle.EnemyNum = 1;
	gRecvMsgBattle.EnemyPetNum = 0;

	if ( team )
	{
		if ( !team->IsLeader( player1 ) )
		{
			assert( 0 );
			return F_FALSE;
		}

		fint32 count = team->GetMemberCount();
		gRecvMsgBattle.EnemyNum = count;

		for ( fint32 i = 0 ; i < count ; i++ )
		{
			CreatureData& data = team->GetMember( i )->GetData();
			CreatureFightData& fightData = team->GetMember( i )->GetFightData();

			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].ActionID = data.ActionID;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].Role = data.Role;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].Color = data.Color;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].Level = data.Level;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].Index = i;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].HP = fightData.Base.HP;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + i ].MAXHP = fightData.Base.MAXHP;
		}

		for ( fint32 i = 0 ; i < count ; i++ )
		{
			GamePet* pet = ((GamePlayer*)team->mMembers[i])->GetAttackPet();

			if ( pet )
			{
				CreatureData& data = pet->GetData();
				CreatureFightData& fightData = pet->GetFightData();

				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].ActionID = data.ActionID;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].Role = data.Role;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].Color = data.Color;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].Level = data.Level;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].Index = i;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].HP = fightData.Base.HP;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + count + gRecvMsgBattle.EnemyPetNum ].MAXHP = fightData.Base.MAXHP;

				gRecvMsgBattle.EnemyPetNum++;
			}

		}
	}
	else
	{
		CreatureData& data = player1->GetData();
		CreatureFightData& fightData = player1->GetFightData();

		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].ActionID = data.ActionID;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Role = data.Role;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Color = data.Color;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Level = data.Level;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Index = 0;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].HP = fightData.Base.HP;
		gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].MAXHP = fightData.Base.MAXHP;

		GamePet* pet = player1->GetAttackPet();

		if ( pet )
		{
			CreatureData& data = pet->GetData();
			CreatureFightData& fightData = pet->GetFightData();

			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].ActionID = data.ActionID;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].Role = data.Role;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].Color = data.Color;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].Level = data.Level;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].Index = 0;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].HP = fightData.Base.HP;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum + 1 ].MAXHP = fightData.Base.MAXHP;

			gRecvMsgBattle.EnemyPetNum = 1;
		}

		gRecvMsgBattle.EnemyNum = 1;
	}

	gRecvMsgBattle.Pos = player2->mMoveData.Pos;
	gRecvMsgBattle.size = 17 + ( gRecvMsgBattle.PetNum + gRecvMsgBattle.EnemyNum + gRecvMsgBattle.EnemyPetNum )* sizeof( MsgCreature );

	player2->PackMsg( &gRecvMsgBattle );
	player2->SendMsg();
	player2->StartFight();
	team = player2->GetTeam();

	if ( team )
	{
		fint32 count = team->GetMemberCount();

		for ( fint32 i = 1 ; i < count ; i++ )
		{
			team->mMembers[ i ]->PackMsg( &gRecvMsgBattle );
			team->mMembers[ i ]->SendMsg();
			team->mMembers[ i ]->StartFight();
		}
	}

    return F_TRUE;
}


fbool				BattleManager::CreatBattle( fint32 ec , GamePlayer* player )
{
	GameEnemyCount* gec = gMapManager->GetGameEnemyCount( ec );
	
	fint32 min = 0;
	fint32 max = 0;
	fint32 rand = 0;
	fint32 groupID = 0;
	fint32 num = MathBase::GetRandomInt( 1 , gec->intValue[ GECIV_NUM ] );
	for ( fint32 i = GECIV_GROUPPRO0 ; i < GECIV_GROUPPRO9 + 1 ; ++i )
	{
		max += gec->intValue[ i ];
	}

	rand = MathBase::GetRandomInt( min , max );

	for ( fint32 i = GECIV_GROUP0 ; i < GECIV_GROUP9 + 1 ; ++i )
	{
		if ( rand < gec->intValue[ i + 10 ] )
		{
			groupID = gec->intValue[ i ];
			break;
		}
		else
		{
			rand -= gec->intValue[ i + 10 ];
		}
	}

	GameEnemyGroup* group = gCreatureManager->GetEnemyGroup( groupID );

	if ( !group )
	{
		return F_FALSE;
	}

	if ( group->intValue[ GEGIV_ITEM0 ] != INVALID_ID )
	{
		// ,, no item no battle.
		return F_FALSE;
	}

	GameEnemy* enemy[ MAX_BATTLE_ENEMY ];

	fint32 index = GEGIV_ENEMY0;
	for ( fint32 i = 0 ; i < num ; ++i )
	{
		if ( group->intValue[ index ] == 0 || index == GEGIV_ENEMYPRO0 )
		{
			index = GEGIV_ENEMY0;
		}
		
		enemy[ i ] = gCreatureManager->GetEnemy( group->intValue[ index ] );
		index++;
	}

	BattleSession* session = CreatSession();

	if ( !session )
	{
		return F_FALSE;
	}

	PlayerSessionMapIter iter = mPlayerMap.find( player );
	if ( iter != mPlayerMap.end() )
	{
		assert( 0 );
		return F_FALSE;
	}

	mPlayerMap[ player ] = session;

	session->InitPVE( player , num , 0 );

	gRecvMsgBattle.PVP = 0;
	gRecvMsgBattle.PetNum = 0;
	GameTeam* team = player->GetTeam();

	if ( team )
	{
		if ( !team->IsLeader( player ) )
		{
			assert( 0 );
			return F_FALSE;
		}

		fint32 count = team->GetMemberCount();
		for ( fint32 i = 0 ; i < count ; i++ )
		{
			GamePet* pet = ((GamePlayer*)team->mMembers[i])->GetAttackPet();

			if ( pet )
			{
				CreatureData& data = pet->GetData();
				CreatureFightData& fightData = pet->GetFightData();

				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].ActionID = data.ActionID;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Role = data.Role;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Color = data.Color;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Level = data.Level;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Index = i;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].HP = fightData.Base.HP;
				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].MAXHP = fightData.Base.MAXHP;

				gRecvMsgBattle.PetNum++;
			}
			
		}
	}
	else
	{
		GamePet* pet = player->GetAttackPet();

		if ( pet )
		{
			CreatureData& data = pet->GetData();
			CreatureFightData& fightData = pet->GetFightData();

			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].ActionID = data.ActionID;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Role = data.Role;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Color = data.Color;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Level = data.Level;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Index = 0;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].HP = fightData.Base.HP;
			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].MAXHP = fightData.Base.MAXHP;

			gRecvMsgBattle.PetNum = 1;
		}
	}

    
    for ( fint32 i = 0 ; i < num ; ++i )
    {
        GameEnemyEntity* enemyEntity = session->GetEnemy( i );
        enemyEntity->Init( enemy[ i ] );
        enemyEntity->UpdateFightData();
        
        enemyEntity->mCommand.Index = (fbyte)i;
        enemyEntity->mCommand.Side = (fbyte)SABSS_ENEMY;
        enemyEntity->mCommand.Row = (fbyte)SABSR_UNDER;
        
        gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].ActionID = enemyEntity->mData.ActionID;
        gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].Role = enemyEntity->mData.Role;
        gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].Color = enemyEntity->mData.Color;
        gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].HP = enemyEntity->mData.Base.MAXHP;
        gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].MAXHP = enemyEntity->mData.Base.MAXHP;
        gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].Level = enemyEntity->mData.Level;
        gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].Index = i;
    }
	

	gRecvMsgBattle.Pos = player->mMoveData.Pos;
	gRecvMsgBattle.EnemyNum = num;
	gRecvMsgBattle.EnemyPetNum = 0;
	gRecvMsgBattle.size = 17 + ( gRecvMsgBattle.PetNum + gRecvMsgBattle.EnemyNum + gRecvMsgBattle.EnemyPetNum )* sizeof( MsgCreature );

	player->PackMsg( &gRecvMsgBattle );
	player->SendMsg();
	player->StartFight();

	if ( team )
	{
		fint32 count = team->GetMemberCount();
		
		for ( fint32 i = 1 ; i < count ; i++ )
		{
			team->mMembers[i]->PackMsg( &gRecvMsgBattle );
			team->mMembers[i]->SendMsg();
			team->mMembers[i]->StartFight();
		}
	}

	return F_TRUE;
}

fbool				BattleManager::CreatBattle( NpcCreateData* data , GamePlayer* player )
{
//	BattleSession* session = CreatSession();
//
//	if ( !session )
//	{
//		return F_FALSE;
//	}
//
//	PlayerSessionMapIter iter = mPlayerMap.find( player );
//	if ( iter != mPlayerMap.end() )
//	{
//		assert( 0 );
//		return F_FALSE;
//	}
//
//	mPlayerMap[ player ] = session;
//
//	fint32 size = (fint32)data->EnemyNo.size();
//	fint32 size1 = (fint32)data->EnemyPetNo.size();
//
//	session->InitPVE( player , size , size1 );
//
//	gRecvMsgBattle.PVP = 0;
//	gRecvMsgBattle.PetNum = 0;
//    
//	GameTeam* team = player->GetTeam();
//
//	if ( team )
//	{
//		if ( !team->IsLeader( player ) )
//		{
//			assert( 0 );
//			return F_FALSE;
//		}
//
//		fint32 count = team->GetMemberCount();
//		for ( fint32 i = 0 ; i < count ; i++ )
//		{
//			GamePet* pet = ((GamePlayer*)team->mMembers[i])->GetAttackPet();
//
//			if ( pet )
//			{
//				CreatureData& data = pet->GetData();
//				CreatureFightData& fightData = pet->GetFightData();
//
//				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].ActionID = data.ActionID;
//				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Role = data.Role;
//				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Color = data.Color;
//				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Level = data.Level;
//				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Index = i;
//				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].HP = fightData.Base.HP;
//				gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].MAXHP = fightData.Base.MAXHP;
//                gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].RidingPet = data.RidingPet;
//                gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].WeaponType = data.WeaponType;
//                
//				gRecvMsgBattle.PetNum++;
//			}
//
//		}
//	}
//	else
//	{
//		GamePet* pet = player->GetAttackPet();
//
//		if ( pet )
//		{
//			CreatureData& data = pet->GetData();
//			CreatureFightData& fightData = pet->GetFightData();
//
//			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].ActionID = data.ActionID;
//			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Role = data.Role;
//			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Color = data.Color;
//			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Level = data.Level;
//			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].Index = 0;
//			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].HP = fightData.Base.HP;
//			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].MAXHP = fightData.Base.MAXHP;
//            gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].RidingPet = data.RidingPet;
//			gRecvMsgBattle.Creature[ gRecvMsgBattle.PetNum ].WeaponType = data.WeaponType;
//            
//
//			gRecvMsgBattle.PetNum = 1;
//		}
//	}
//
//	for ( fint32 i = 0 ; i < size ; ++i )
//	{
//		GameEnemyEntity* enemyEntity = session->GetEnemy( i );
//		enemyEntity->Init( gCreatureManager->GetEnemy( data->EnemyNo[i] ) );
//		enemyEntity->UpdateFightData();
//		enemyEntity->mCommand.Index = (fbyte)i;
//		enemyEntity->mCommand.Side = (fbyte)SABSS_ENEMY;
//		enemyEntity->mCommand.Row = (fbyte)SABSR_UNDER;
//
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].ActionID = enemyEntity->mData.ActionID;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].Role = enemyEntity->mData.Role;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].Color = enemyEntity->mData.Color;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].HP = enemyEntity->mData.Base.MAXHP;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].MAXHP = enemyEntity->mData.Base.MAXHP;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].Level = enemyEntity->mData.Level;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].Index = i;
//        gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].RidingPet = enemyEntity->mData.RidingPet;
//        gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum ].WeaponType = enemyEntity->mData.WeaponType;
//	}
//	
//	for ( fint32 i = 0 ; i < size1 ; ++i )
//	{
//		GameEnemyEntity* enemyEntity = session->GetEnemyPet( i );
//		enemyEntity->Init( gCreatureManager->GetEnemy( data->EnemyPetNo[i] ) );
//		enemyEntity->UpdateFightData();
//		enemyEntity->mCommand.Index = (fbyte)i;
//		enemyEntity->mCommand.Side = (fbyte)SABSS_ENEMY;
//		enemyEntity->mCommand.Row = (fbyte)SABSR_UP;
//		
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum + size ].ActionID = enemyEntity->mData.ActionID;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum + size ].Role = enemyEntity->mData.Role;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum + size ].Color = enemyEntity->mData.Color;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum + size ].HP = enemyEntity->mData.Base.MAXHP;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum + size ].MAXHP = enemyEntity->mData.Base.MAXHP;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum + size ].Level = enemyEntity->mData.Level;
//		gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum + size ].Index = i;
//        
//        gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum + size ].RidingPet = enemyEntity->mData.RidingPet;
//        gRecvMsgBattle.Creature[ i + gRecvMsgBattle.PetNum + size ].WeaponType = enemyEntity->mData.WeaponType;
//
//	}
//
//	gRecvMsgBattle.Pos = player->mMoveData.Pos;
//	gRecvMsgBattle.EnemyNum = size;
//	gRecvMsgBattle.EnemyPetNum = size1;
//	gRecvMsgBattle.size = 17 + ( gRecvMsgBattle.PetNum + gRecvMsgBattle.EnemyNum + gRecvMsgBattle.EnemyPetNum )* sizeof( MsgCreature );
//	
//	player->PackMsg( &gRecvMsgBattle );
//	player->SendMsg();
//	player->StartFight();
//
//	if ( team )
//	{
//		fint32 count = team->GetMemberCount();
//
//		for ( fint32 i = 1 ; i < count ; i++ )
//		{
//			team->mMembers[i]->PackMsg( &gRecvMsgBattle );
//			team->mMembers[i]->SendMsg();
//			team->mMembers[i]->StartFight();
//		}
//	}
	
	return F_TRUE;
}

BattleSession*		BattleManager::CreatSession()
{
	if ( mFreeSession.empty() )
	{
		return F_NULL;
	}

	BattleSession* session = mFreeSession[ mFreeSession.size() - 1 ];
	mFreeSession.erase( mFreeSession.begin() + mFreeSession.size() - 1 );

	mSessionList.push_back( session );
	// init session 

	return session;
}

fvoid				BattleManager::ReleaseSession( BattleSession* session )
{
	RemovePlayer( session->mPlayer[ 0 ] );
	
	if ( session->mNumberPlayerPVP )
		RemovePlayer( session->mPlayerPVP[ 0 ] );
	
	session->Release();
	
	mSessionList.remove( session );
	mFreeSession.push_back( session );
}


fvoid				BattleManager::RemovePlayer( GamePlayer* player )
{
	PlayerSessionMapIter iter = mPlayerMap.find( player );
	
	if ( iter == mPlayerMap.end() )
	{
		assert( 0 );
	}
	
	mPlayerMap.erase( iter );
}


BattleSession*		BattleManager::GetSession( GamePlayer* player )
{
	PlayerSessionMapIter iter = mPlayerMap.find( player );

	if ( iter == mPlayerMap.end() )
	{
		return F_NULL;
	}

	return iter->second;
}


// fvoid				BattleManager::Update( freal32 delay )
// {
// 	SessionListIter iterEnd = mSessionList.end();
// 
// 	for ( SessionListIter iter = mSessionList.begin() ; iter != iterEnd ;  )
// 	{
// 		
// 		if ( (*iter)->Update( delay ) )
// 		{
// 			(*iter)->Release();
// 			mFreeSession.push_back( (*iter) );
// 			iter = mSessionList.erase( iter );
// 		}
// 		else
// 		{
// 			++iter;
// 		}
// 
// 
// 	}
// }
